
# 导入随机模块
import random
# 导入settings文件中的UPPOOL
# from .settings import UAPOOL(报错)
from taobao.settings import UAPOOL
# 导入官方文档对应的HttpProxyMiddleware
# from scrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware     (错误原因：旧模块被弃用，改动了两个地方)
# from scrapy.downloadermiddleware.useragent import UserAgentMiddleware
from scrapy.downloadermiddlewares.useragent import UserAgentMiddleware

class Uamid(UserAgentMiddleware):
    def __init__(self,user_agent=''):
        self.user_agent = user_agent
    def proccess_request(self,request,spider):
        thisua = random.choice(UAPOOL)
        print("当前使用的 User-Agent 是：" + thisua)
        request.headers.setdefault('User-Agent',thisua)